package com.iflytek.ssm.erp.controller;

import com.iflytek.ssm.erp.entity.Customer;
import com.iflytek.ssm.erp.service.CustomerService;
import com.iflytek.ssm.util.ReturnUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    private static final Logger log = LoggerFactory.getLogger(CustomerController.class);

    @Autowired
    private CustomerService customerService;

    /**
     * 获取客户信息接口
     */
    @GetMapping("/list")
    public Object listCustomer(String name,String contactPerson,String mobile,String phone) {
        // 本层做数据校验
        // TODO: 获取客户信息
        List<Customer> list = customerService.list(name, contactPerson, mobile, phone);
        log.info("获取客户信息成功");
        log.info("list={}", list);
        return ReturnUtil.success(list);
    }


    @GetMapping("/add")
    public Object addCustomer(Customer customer) {
        // 本层做数据校验
        // TODO: 添加客户信息
        boolean flag = customerService.addCustomer(customer);
        if (!flag) {
            log.error("添加客户信息失败");
            return ReturnUtil.fail("添加失败");
        }
        return ReturnUtil.success("添加成功");
    }

    @GetMapping("/update")
    public Object updateCustomer(Customer customer) {
        // 本层做数据校验
        // TODO: 修改客户信息
        boolean flag = customerService.updateCustomer(customer);
        if (!flag) {
            log.error("修改客户信息失败");
            return ReturnUtil.fail("修改失败");
        }
        log.info("修改客户信息成功");
        log.info("customer={}", customer);
        return ReturnUtil.success("修改成功");
    }

    @GetMapping("/delete")
    @ResponseBody
    public Object deleteCustomer(Integer id) {
        // 本层做数据校验
        // TODO: 删除客户信息
        boolean flag = customerService.deleteById(id);
        if (!flag) {
            log.error("删除客户信息失败");
            return ReturnUtil.fail("删除失败");
        }
        return ReturnUtil.success("删除成功");
    }

    /**
     * 根据ID查询客户信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Object queryCustomer(@PathVariable("id") Integer id) {
        // 本层做数据校验
        // TODO: 查询客户信息
        Customer customer = customerService.queryById(id);
        if (customer == null) {
            log.error("查询客户信息失败");
            return ReturnUtil.fail("查询失败");
        }

        log.info("查询客户信息成功");
        log.info("customer={}", customer);
        return ReturnUtil.success(customer);
    }




}
